<?php

/**
 * LogView Class
 * Outputs the requested LogEntries with HTML markup
 */
class LogView {
	
	//div class names
	const WARNING_DIV_CLASS = 'warning_log';
	const ERROR_DIV_CLASS = 'error_log';
	const MESSAGE_DIV_CLASS = 'message_log';
	
	/**
	 * LogView::showAllSavedErrors()
	 * @return string Outputs a list of the errors stored in the error file
	 */
	public static function showAllSavedErrors()
	{
		//Read the file
		if(file_exists(Log::ERROR_FILE))
			$fileContent = file_get_contents(Log::ERROR_FILE);
		else {
			Log::logError(Log::ERROR_FILE . " error file doesn't exist!");
			return null;
		}
		
		//Get an array of each line
		$lines = explode("\n", trim($fileContent));
		$numberOfErrors = count($lines);
		
		//Start constructing the div
		$output = '<div class="' . LogView::ERROR_DIV_CLASS . '">';
		
		if ( $numberOfErrors > 0){		//If we got errors output them
			
			$output .=  "<h1>There are $numberOfErrors error(s).</h1>";
			$output .= "<ul>";
			
			foreach ($lines as $error){
				
				//Parse the individual line so that we extract the error information
				$errorLog = LogError::parse($error);
				
				//If successuly parsed list the error content
				if(!is_null($errorLog)){
					
					$output .= '<li>
									<h2>' . $errorLog['time'] . '=> ' . $errorLog['msg'] . '</h2>' .
									self::getUL($errorLog) .
								'</li>';
				}
			}
			
			$output .= '</ul>';
		}
		else {	//No errors recorded
			$output .= "<p>There are no errors.</p>";
		}
		$output .= '</div>';
		
		return $output;
	}
	
	/**
	 * LogView::showWarnings()
	 * @return string Outputs a list of the logged warnings
	 */
	public static function showWarnings()
	{
		return self::showEntriesOfType(Log::WARNING, self::WARNING_DIV_CLASS);
	}
	
	/**
	 * LogView::showMessages()
	 * @return string Outputs a list of the logged messages
	 */
	public static function showMessages()
	{
		return self::showEntriesOfType(Log::MESSAGE, self::MESSAGE_DIV_CLASS);
	}
	
	/**
	 * LogView::showErrors()
	 * @return string Outputs a list of the logged errors (in Memory)
	 */
	public static function showErrors()
	{
		return self::showEntriesOfType(Log::ERROR, self::ERROR_DIV_CLASS);
	}
	
	/**
	 * LogView::showAllEntries()
	 * @return string Outputs a list of all the entries logged
	 */
	public static function showAllEntries() 
	{
		$output = LogView::showAllSavedErrors();
		$output .= LogView::showWarnings();
		$output .= LogView::showMessages();
		
		return $output;
	}
	
	/**
	 * LogView::showEntriesOfType()
	 * A help function that generates a generic list of entries
	 * @return string Outputs a list of the desired entryType
	 */
	protected static function showEntriesOfType($entryType = Log::MESSAGE, $divClass = self::MESSAGE_DIV_CLASS)
	{
		$entries = Log::getEntriesOfType($entryType);
		$typeToLower = strtolower($entryType);

		$numberOfEntries = count($entries);
		
		$output = '<div class="' . $divClass . '">';
		
		if ($numberOfEntries > 0){
			
			$output .=  "<h1>There are $numberOfEntries $typeToLower(s).</h1>";
			
			$output .= self::getUL($entries);
		}
		else {
			$output .= "<p>There are no {$typeToLower}s.</p>";
		}
		$output .= '</div>';
		
		return $output;
	}
	
	/**
	 * LogView::getUL()
	 * A help function that generates a <ul></ul> html listing for the given array
	 * @return string The ul list
	 */
	protected static function getUL($array)
	{	
		$list = '';
		
		if(count($array) > 0){
			$list .= '<ul>';
			foreach ($array as $key => $value)
				$list .= "<li>" . (is_numeric($key) ? '' : "<b>{$key}:</b>") . 
									(is_array($value) ? ('<pre>' . var_export($value, true) . '</pre>') : $value) . "</li>";
			
			$list .= '</ul>';
		}
		
		return $list;
	}
}
